package com.example.jam.musicplayer.http;

import android.net.http.HttpsConnection;
import android.support.annotation.Nullable;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;

public class LrcFileDownLoad {

    public static final String LRC_SEARCH_URL = "http://geci.me./api/lyric/";
    private static final int REQUEST_TIMEOUT = 15*1000;
    private static final int SO_TIMEOUT = 15*1000;

    public static String getSongLRCUrl(String path,String songName)
    throws Exception{
        String url = null;
        String str_json = null;

        //歌曲名为空返回null
        if(songName == null)
        {
            return null;
        }
        //歌名编码转换
        String name = URLEncoder.encode(songName,"UTF-8");
        String nameFinal = name.replace("+"," ");
        str_json = getHtmlCode(path + nameFinal);

        if(str_json == null) return null;

        JSONObject jsonObject = new JSONObject(str_json);
        int count = jsonObject.getInt("count");

        //找不到歌词返回null
        if(count == 0){
            return null;
        }

        //获取第一个歌词
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        JSONObject item = jsonArray.getJSONObject(0);

        url = item.getString("lrc");
        return url;
    }

    //设置超时时间
    public static HttpClient getHttpClient(){
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams,SO_TIMEOUT);
        HttpClient client = new DefaultHttpClient(httpParams);
        return client;
    }

    //获取网页源码
    @Nullable
    public static String getHtmlCode(String path){
        String result = null;
        try{
//            HttpClient httpClient = getHttpClient();
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(path);
            HttpResponse response = httpClient.execute(get);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                HttpEntity entity = response.getEntity();
                BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(),"utf-8"));
                String line;
                result = "";
                while((line = br.readLine()) != null){
                    result += line + "\n";
                }
            }
        }catch(ConnectTimeoutException e)
        {
            System.out.println("ConnectTimeoutException timeout");
            return null;
        }catch(SocketTimeoutException e)
        {
            System.out.println("SocketTimeException timeout");;
            return null;
        }catch(Exception e){
            System.out.println(e.getMessage() + ":" + e.toString());
            return null;
        }
        return result;
    }

}
